package com.campus.manage.service.impl;

import com.campus.manage.domain.News;
import com.campus.manage.service.IAliyunOpenAIService;
import com.campus.manage.service.INewsAIService;
import com.campus.manage.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class NewsAIServiceImpl implements INewsAIService {

    @Autowired
    private INewsService newsService;

    @Autowired
    private IAliyunOpenAIService aliyunOpenAIService;

    @Override
    public News selectNewsByNewsId(Long newsId) {
        return newsService.selectNewsByNewsId(newsId);
    }

    @Override
    public List<News> selectNewsList(News news) {
        return newsService.selectNewsList(news);
    }

    @Override
    public int insertNews(News news) {
        return newsService.insertNews(news);
    }

    @Override
    public int updateNews(News news) {
        return newsService.updateNews(news);
    }

    @Override
    public int deleteNewsByNewsIds(Long[] newsIds) {
        return newsService.deleteNewsByNewsIds(newsIds);
    }

    @Override
    public int deleteNewsByNewsId(Long newsId) {
        return newsService.deleteNewsByNewsId(newsId);
    }

    @Override
    public String generateNewsSummary(Long newsId) {
        try {
            // 1. 根据newsId获取新闻内容
            News news = newsService.selectNewsByNewsId(newsId);
            if (news == null) {
                throw new RuntimeException("未找到对应的新闻，newsId: " + newsId);
            }

            // 2. 检查新闻内容是否为空
            if (news.getContent() == null || news.getContent().trim().isEmpty()) {
                throw new RuntimeException("新闻内容为空，无法生成摘要");
            }

            // 3. 调用阿里云服务生成摘要
            String summary = aliyunOpenAIService.generateSummary(
                    news.getNewsTitle() != null ? news.getNewsTitle() : "无标题",
                    news.getContent()
            );

            return summary;

        } catch (Exception e) {
            throw new RuntimeException("生成新闻摘要失败: " + e.getMessage(), e);
        }
    }
}